import { ValidatorFactory } from "../../../validator";

function validateLength(name: string) {
    if (name.length === 0) {
        return { data: name, success: false };
    }
    return { data: name, success: true };
}

function validateChars(name: string) {
    // 检查文件名是否包含非法字符
    const invalidChars = ["/", "\\", "?", "%", "*", ":", "|", '"', "<", ">", "."];
    for (const char of invalidChars) {
        if (name.includes(char)) {
            return {
                data: name,
                success: false,
                message: `Invalid character in file name: ${char}`
            };
        }
    }
    // 检查文件名是否以空格或点开头或结尾
    if (name.startsWith(" ") || name.endsWith(" ") || name.startsWith(".") || name.endsWith(".")) {
        return {
            data: name,
            success: false,
            message: "File name cannot start or end with space or dot"
        };
    }
    return { data: name, success: true };
}

export const fileNameValidator = new ValidatorFactory<string>({
    validateLength,
    validateChars
}).createValidator();

